﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SystemMHB.Infrstructer
{
    /// <summary>
    /// 总仓储
    /// </summary>
    public class BeanRepository<T> : IRepository<T> where T : class, new()
    {
        /// <summary>
        /// 上下文
        /// </summary>
        ApplicationDbContext db;
        /// <summary>
        /// 构造函数
        /// </summary>
        public BeanRepository(ApplicationDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加数据信息
        /// </summary>
        /// <param name="expre">要添加的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddAsync(T expre)
        {
            db.Set<T>().Add(expre);
           return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 显示全部数据信息
        /// </summary>
        /// <returns>返回数据信息集合</returns>
        public async Task<List<T>> GetAllAsync()
        {
            return await db.Set<T>().ToListAsync();
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>返回数据信息</returns>
        public async Task<T> GetModelAsync(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 修改数据信息（逻辑删除）可以批量删除
        /// </summary>
        /// <param name="expre">修改后的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateAsync(T expre)
        { 
            db.Entry(expre).State = EntityState.Modified;
            return await db.SaveChangesAsync();
        
        }
    }
}
